<% can_edit = @team.name == Team::ADMINS ? manages_admins? : manages_team_members?(@team) %>
<%= grouped_list(@team.groups.includes(:users).all,
                 :new_link => can_edit && link_to_function(icon('plus', 'Add group...'), 'show_add_team_member_dialog("group")'),
                 :collapse => true,
                 :toolbar  => {:list_name => 'team_group_list',
                               :filter_by => %w(username group)}) do |group_builder, group| %>
  <% group_builder.title do %>
    <%= group.name %>
    &nbsp;&nbsp;<small><span class="muted"><%= pluralize(group.users.size, 'user') %></span></small>
  <% end %>
  <% if can_edit %>
    <% group_builder.menu do %>
      <a class="dropdown-toggle" data-toggle="dropdown" href="#"><span><%= icon('ellipsis-v') %>&nbsp;</span></a>
      <ul class="dropdown-menu" role="menu">
        <li>
          <%= link_confirm_busy(icon('remove', 'Remove'),
                                :url     => organization_team_member_path(@team, group.name, :type => 'group'),
                                :method  => :delete,
                                :confirm => 'Please confirm',
                                :body    => "Remove group '#{group.name}' from team '#{@team.name}'?") %>
        </li>
      </ul>
    <% end %>
  <% end %>
  <%= group_builder.items(group.users) do |builder, user| %>
    <% builder.itemid user.id %>
    <% builder.attributes raw(%(username="#{user.username}" group="#{group.name}")) %>
    <% builder.top do %>
      <span class="topname"><%= user.username %></span>
      <span class="topdesc"><%= user.email %></span>
    <% end %>
    <% full_name = user.name
       builder.middle full_name if full_name.present? %>
  <% end %>
<% end %>

